﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommDll;

namespace SupervisionCodeCollection
{
    /// <summary>
    /// ClassName:frmDbConfig
    /// Version:1.0
    /// Date:2016-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现监管码信息设置
    /// </remarks>
    public partial class frmGoodsCodeSetup : DevComponents.DotNetBar.OfficeForm
    {
        #region 1.自定义属性  
        public string m_strSupervisionCode = "";    //电监码
        public Boolean m_bIsNew = true;
        public string m_strGoodsCode = "", m_strGoodsName = "";
        public DataTable m_dt = null;
        
        #endregion

        public frmGoodsCodeSetup()
        {
            InitializeComponent();
        }

        #region 2.系统事件方法
        private void frm_Load(object sender, EventArgs e)
        {
            _init();            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _saveInfo();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Key_Down(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //this.SelectNextControl(this.ActiveControl,true, true, true, true);
                SendKeys.Send("{Tab}");  //向活动应用程序发送击键 注意格式:Send("{Tab}");中的{}
            }
        }

        #endregion

        #region  3.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 01 取数据
            txtSupervisionCode.Text = m_strSupervisionCode;            
            if (m_bIsNew)
            {
                string strSql = "select * from TH_GoodsSupervisionCode where SupervisionCode = '{0}'";
                strSql = string.Format(strSql, m_strSupervisionCode);                
                if (AppInfo.m_bIsWebserverDB)
                {
                    m_dt = AccessDB_WS.AccessDB._openSql_dt(strSql);
                }
                else
                {
                    //m_dt = DBAccess._openSQL_dt(strSql);
                }
                //txtOperateName.Text = User.strName;
                txtOperateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else{
                txtGoodsCode.Text = m_dt.Rows[0]["GoodsCode"].ToString();
                txtName.Text = m_dt.Rows[0]["Name"].ToString();
                txtSpec.Text = m_dt.Rows[0]["Spec"].ToString();
                txtPackage.Text = m_dt.Rows[0]["Package"].ToString();
                txtAmount.Text = m_dt.Rows[0]["Amount"].ToString();
                txtForm.Text = m_dt.Rows[0]["Form"].ToString();
                txtApprovalNumber.Text = m_dt.Rows[0]["ApprovalNumber"].ToString();
                txtManufacturer.Text = m_dt.Rows[0]["Manufacturer"].ToString();
                txtOperateName.Text = m_dt.Rows[0]["OperateName"].ToString();
                txtOperateTime.Text = m_dt.Rows[0]["OperateTime"].ToString();
            }
            txtSupervisionCode.Focus();
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        private void _saveInfo() {
            // 01 数据非空验证
            if (txtGoodsCode.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("商品电话不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtName.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("商品名称不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtSpec.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("商品规格不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtPackage.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("包装不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtAmount.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("数量不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtForm.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("剂型不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtApprovalNumber.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("批准文号不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtManufacturer.Text.Trim().Equals(""))
            {
                CommFun._messageBoxShow("生产厂家不能为空，请重新输入！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string strSql = "Insert into TH_GoodsSupervisionCode(SupervisionCode,GoodsCode,Name,Form,Spec,ApprovalNumber,Package,Amount,Manufacturer,Status,OperateName,OperateTime)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','0','{9}',getDate())";
            if (!m_bIsNew)
            {
                // 修改
                strSql = "Update TH_GoodsSupervisionCode set GoodsCode = '{1}',Name = '{2}',Form = '{3}',Spec = '{4}',ApprovalNumber = '{5}',Package = '{6}',Amount = '{7}',Manufacturer = '{8}',OperateTime = getDate(),OperateName = '{9}' where SupervisionCode = '{0}'";
            }
            strSql = string.Format(strSql
                ,txtSupervisionCode.Text.Trim()
                , txtGoodsCode.Text.Trim()
                , txtName.Text.Trim()
                , txtForm.Text.Trim()
                , txtSpec.Text.Trim()
                , txtApprovalNumber.Text.Trim()                
                , txtPackage.Text.Trim()
                , txtAmount.Text.Trim()
                , txtManufacturer.Text.Trim()
                ,""//User.strName
                );
            
            // 02 保存数据
            int nResult = 0;
            if (AppInfo.m_bIsWebserverDB)
            {
                nResult = AccessDB_WS.AccessDB._execSQL(strSql);
            }
            else
            {
                //nResult = DBAccess._execSQL(strSql);
            } 
            if (nResult==1)
            {
                CommFun._messageBoxShow("商品电子监管码信息保存成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                m_strGoodsCode = txtGoodsCode.Text.Trim();
                m_strGoodsName = txtName.Text.Trim();
                this.DialogResult = DialogResult.OK;
            }
            else{
                CommFun._messageBoxShow("商品电子监管码信息保存失败！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        #endregion

        private void btnQuery_Click(object sender, EventArgs e)
        {
            // 取商品编号
            ////frmQuerySelect frm = new frmQuerySelect();
            //if (frm.ShowDialog() == DialogResult.OK) {
            //    txtGoodsCode.Text = frm.m_dgvrCurrentRow.Cells["Code"].Value.ToString();
            //    if (m_bIsNew)
            //    {
            //        txtName.Text = frm.m_dgvrCurrentRow.Cells["Name"].Value.ToString();
            //        txtSpec.Text = frm.m_dgvrCurrentRow.Cells["PackageSpec"].Value.ToString();
            //        txtPackage.Text = "";
            //        txtAmount.Text = "1";
            //        txtForm.Text = frm.m_dgvrCurrentRow.Cells["Form"].Value.ToString();
            //        txtApprovalNumber.Text = frm.m_dgvrCurrentRow.Cells["ApprovalNumber"].Value.ToString();
            //        txtManufacturer.Text = frm.m_dgvrCurrentRow.Cells["Manufacturer"].Value.ToString();
            //    }
            //}
        }
    }
}